var app = getApp();
Page({
    data: {

        token:"",
        userInfo: "",
        cardA: "", // 身份证
        personInfo: {}, // 身份证信息
        cardB: "", // 身份证
        cardImg: "", // 营业执照
        licenseInfo: {}, 
        region: [],
        regionFlag: false,
        regionA: [],
        rightIcon: "/public/icon/chevron_right_btn.png",
        
        uploadIcon: "/public/icon/upload_btn.jpg",
        uploadIcon1: "/public/icon/upload_btn_1.png",
        uploadIcon2: "/public/icon/upload_btn_2.png",
        uploadIcon3: "/public/icon/upload_btn_3.png",
        industryArr: ["家具", "建材", "软装设计", "地产", "定制", "整装(包家具)", "整装", "窗帘灯饰", "大门店", "夫妻店", "设计师个人"],
        submitInfo: {},      
        submitFlag: 0,
        examineFlag: 0,

        industryIndex: 0,
        userTypeArr:["", "个人", "企业", "个体工商户"],
        userType: "",  // 签约主体
        imgIcon: "/public/icon/pay_successful.png"
    },
    onShow () {
      this.init()
      this.getSettledFn()
  },
  init: function () {
      let  userInfo = wx.getStorageSync("userInfo");
      let token = userInfo.access_id;

      
      let submitFlag = userInfo.submitFlag;
      let examineFlag = userInfo.examineFlag;
      this.setData({
          userInfo: userInfo,

          token: token,
          submitFlag: submitFlag,
          examineFlag: examineFlag
      })
  },
    selectIndustryFn: function (e) {
         let value = e.detail.value;
         this.setData({
             industryIndex: value
         })
    },
    selectImgFn: function (e) {
      let _this = this;
      let imgType = e.currentTarget.dataset.type; 
      wx.chooseImage({
        count: 5,
        sizeType: ['original', 'compressed'],
        sourceType: ['album', 'camera'],
        success (res) {
              let url = res.tempFilePaths[0];
              _this.uploadImgFn(url, imgType)
        }
      })
  },
  uploadImgFn: function (file, imgType) {
      let _this = this;
      let token = this.data.token;
      let url = app.globalData.url;
      let upImgArr = this.data.upImgArr;
      wx.showLoading({ 
          title: "加载中"
      })
      wx.uploadFile({
        url: url,
        filePath: file,
        name: 'file',
        formData: {
          api: "resources.file.uploadFiles",
          accessId: token,  
          storeId: 1,
          storeType: 6
        },
        success (res){
            const resData = JSON.parse(res.data);
            let  imgUrl = "";
            if (resData) {
                wx.hideLoading() 
                let option = {};
                imgUrl = resData.data.imgUrls[0];
                option.imgPath = imgUrl;
                option.imgType = imgType;
                if (imgType == "cardA") {
                    option.ocrType = 1;
                } else if (imgType == 'cardImg') {
                    option.ocrType = 2;
                } else if(imgType == 'cardB') {
                    option.ocrType = 1;
                }      
                let userType = wx.getStorageSync('selectUserType');
                 _this.setData({
                    [imgType]: imgUrl
                    // userType: userType
                 })
                 _this.getImgInfoFn(option)
            }
        },
        fail () {
            _this.showErrorFn("上传失败")
        }
      })
  },
  getImgInfoFn: function (option) {
      let _this = this;
      let token = this.data.token;
      let url = app.globalData.url;
      wx.request({
        url: url, 
        method: "get",
        data: {
          api: 'app.ocr.getOcrInfo',
          storeId: 1,
          storeType: 6,
          accessId: token,
          imgPath: option.imgPath,
          ocrType:  option.ocrType
        },
        header: {
          'content-type': 'application/json'  
        },
        success (res) {
            wx.hideLoading()
            let resData = res.data;
            if (res.data.code == 200) {
              let cardInfo = "";
              if (option.imgType == 'cardA') {
                  if (!res.data.data.data.face) {
                      _this.showErrorFn("请上传人像面")
                      _this.setData({
                          cardA: "",
                          personInfo: ""
                      })     
                      return ;
                  }
                  cardInfo = res.data.data.data.face.data;
                  _this.setData({
                      personInfo: cardInfo
                  }, function () {
                      _this.checkPeopleIDFn()
                  })
              } else if (option.imgType == 'cardImg') {
                  cardInfo = res.data.data.data;
                  _this.setData({
                      licenseInfo: cardInfo 
                  }, function () {
                       _this.checklicenseFn()
                  })       
              } else if (option.imgType == 'cardB') {
                  if (!res.data.data.data.back) {
                      _this.showErrorFn("请上传国徽面")
                      _this.setData({
                          cardB: ""
                      })     
                      return ;
                  }
              } 
          } else {
              if (option.imgType == 'cardA') {
                  _this.showErrorFn("请上传人像面")
                  _this.setData({
                      cardA: "",
                      personInfo: ""
                  })       
              } else if (option.imgType == 'cardImg') {   
                  _this.showErrorFn("营业执照无效")
                  _this.setData({
                      cardImg: "", // 营业执照
                      licenseInfo: "" // 营业执照信息
                  })
              } else if (option.imgType == 'cardB') {
                  _this.showErrorFn("请上传国徽面")
                  _this.setData({
                      cardB: ""
                  })
              }
          }
        }
      })
  },
  checkPeopleIDFn: function () { // check 身份证
    let _this = this;
    let token = this.data.token;
    let url = app.globalData.url;
    let personInfo = this.data.personInfo;
    wx.request({
      url: url, 
      method: "get",
      data: {
        api: 'app.verify.verifyIdCard',
        storeId: 1,
        storeType: 6,
        accessId: token,
        name: personInfo.name,
        idNumber: personInfo.idNumber
      },
      header: {
        'content-type': 'application/json'  
      },
      success (res) {
          wx.hideLoading()
          let resData = res.data;
          if (res.data.code != 200) {
            _this.showErrorFn("身份证无效")
            _this.setData({
                cardA: "",
                personInfo: ""
            })
          }
      }
    })
  },
  checklicenseFn: function () {
    let _this = this;
    let token = this.data.token;
    let url = app.globalData.url;
    let personInfo = this.data.personInfo;
    let licenseInfo = this.data.licenseInfo;
    wx.request({
        url: url, 
        method: "get",
        data: {
          api: 'app.verify.verifyBusinessLicense',
          storeId: 1,
          storeType: 6,
          accessId: token,
          creditCode: licenseInfo.creditCode,
          companyName: licenseInfo.companyName,
          legalPerson: licenseInfo.legalPerson
        },
        header: {
          'content-type': 'application/json'  
        },
        success (res) {
          wx.hideLoading()
          if (res.data.code != 200) {
              if (res.data.code == 51053) {
                _this.showErrorFn("营业执照已被使用")
              } else {

                _this.showErrorFn("营业执照无效")
              }
             
              _this.setData({
                  cardImg: "",
                  licenseInfo: ""
              })
          }
          if (res.data.code == 200) {
              if (!res.data.data.data) {
                  _this.showErrorFn("营业执照无效")   
                  _this.setData({
                      cardImg: "",
                      licenseInfo: ""
                  })  
              }
          }
        }
      })
  },
  bindRegionChange: function (e) {
    this.setData({
      region: e.detail.value
    })
  },
  bindRegionChangeA: function (e) {
    this.setData({
      regionA: e.detail.value
    })
  },
  userTypeFn: function (e) {
      let value = e.detail.value;
      this.setData({
          userType: value
      })
      wx.setStorageSync('selectUserType', value)
  },
  //  签名主体名称
  contractNameFn: function (e) {
      let value = e.detail.value;
      let licenseInfo = this.data.licenseInfo;

      licenseInfo.companyName = value;   
      this.setData({
          licenseInfo: licenseInfo
      })
  },

  // 企业社会信用代码 
   codeFn: function (e) {
      let value = e.detail.value;
      let licenseInfo = this.data.licenseInfo;
      licenseInfo.creditCode = value;
      this.setData({
          licenseInfo: licenseInfo
      })
  },
  // 企业法人姓名
  personUnameFn: function (e) {
      let value = e.detail.value;
      let personInfo = this.data.personInfo;
      personInfo.name = value; 
      this.setData({
          personInfo: personInfo
      })
  },
    // 法人身份证号码
    personIDFn: function (e) {
      let value = e.detail.value;
      let personInfo = this.data.personInfo;
      personInfo.idNumber = value;
      this.setData({
          personInfo: personInfo
      })
  },
  detailAdressFnb: function (e) {
    let licenseInfo = this.data.licenseInfo;
    let value = e.detail.value;
    licenseInfo.businessAddress = value;
    this.setData({
        licenseInfo: licenseInfo
    })
  },
  showErrorFn: function (errorMsg) {
      wx.showToast({       
        title: errorMsg,
        icon: "error",
        mask: true,
        duration: 2000
      })
  },
  submitFn: function () {
      let _this = this;
      let token = this.data.token;
      let url = app.globalData.url;
      let personInfo = this.data.personInfo;
      let licenseInfo = this.data.licenseInfo;
      let industryIndex = this.data.industryIndex;
      let userType = this.data.userType;
      let cardA = this.data.cardA;
      let cardB = this.data.cardB;
      let cardImg = this.data.cardImg;
      if (industryIndex == null) {
          _this.showErrorFn("请选择行业")
          return ;
      }
      if (!cardA || !personInfo) {
          _this.showErrorFn("请上传人像面")
          return ;
      }
      if (!cardB) {
          _this.showErrorFn("请上传国徽面")
          return ;
      }
      if ( !cardImg || !licenseInfo) {
          _this.showErrorFn("请上传营业执照")
          return ;
      }
     console.log("userType", userType)
      if (userType == 2 || userType ==3 ){
         
      } else {
          _this.showErrorFn("请选择签约主体")
          return ;
      }
      if (licenseInfo && !licenseInfo.companyName) {
          _this.showErrorFn("签约主体名称")
          return ;
      }
      if (licenseInfo && !licenseInfo.creditCode) {
          _this.showErrorFn("社会信用代码")
          return ;
      }
      if (personInfo && !personInfo.name) {
          _this.showErrorFn("企业法人姓名")
          return ;
      }
      if (personInfo && !personInfo.idNumber) {
          _this.showErrorFn("法人身份证号码")
          return ;
      }
      if (licenseInfo && !licenseInfo.businessAddress) {
          _this.showErrorFn("请输入详细地址")
          return ;
      }
      if (personInfo.name && personInfo.name != licenseInfo.legalPerson) {
        wx.showModal({
          title: '提示',
          content: "法人身份证与营业执照信息不一致",
          success () {
               
          }
        })   
        return ;
      }   
      if (personInfo && licenseInfo) {
          wx.request({
            url: url, 
            method: "get",
            data: {
              api: 'app.user.submitBusinessInfo',
              storeId: 1,
              storeType: 6,
              accessId: token,

              // signatoryType: userType,
              industry: _this.data.industryIndex, 
              idCardFaceImg: cardA,
              idCardBackImg: cardB,
              businessLicenseImg: cardImg,
              signatoryType: _this.data.userType,
              signatoryName: licenseInfo.companyName,
              companyCreditCode: licenseInfo.creditCode,
              userName: personInfo.name,
              idCard: personInfo.idNumber,
              detailedAddress: licenseInfo.businessAddress
            },
            header: {
              'content-type': 'application/json'  
            },
            success (res) {
                wx.hideLoading()
                let resData = res.data;
                if (res.data.code == 200) {
                      _this.setData({
                          submitFlag: 1,
                          examineFlag: 0
                      })         
                } else {
                    _this.showErrorFn(resData.message)
                }
            }
          })
        }
    },
    submitChangeFn: function () {
      let _this = this;
      let token = this.data.token;
      let url = app.globalData.url;
      let personInfo = this.data.personInfo;
      let licenseInfo = this.data.licenseInfo;
      let industryIndex = this.data.industryIndex;
      let userType = this.data.userType;
      let cardA = this.data.cardA;
      let cardB = this.data.cardB;
      let cardImg = this.data.cardImg;


    //  let userInfoDetail = this.data.userInfoDetail;
      // let industryLabel = this.state.industryLabel;
      wx.request({
        url: url, 
        method: "get",
        data: {
            api: "app.user.submitBusinessInfo",
            accessId: token,
            storeId: 1,
            storeType: 6,
            // signatoryType:  "",
            industry: _this.data.industryIndex,
            idCardFaceImg: "",
            idCardBackImg: "",
            businessLicenseImg: "",
            signatoryType: "",
            signatoryName: "",
            companyCreditCode: "",
            userName: "",
            idCard: "",
            detailedAddress:  "" 
        },
        header: {
          'content-type': 'application/json'  
        },
        success (res) {
            wx.hideLoading()
            let resData = res.data;
            if (res.data.code == 200) {
                wx.showToast({
                  title: resData.message,
                  icon:"success",
                  mask: true,
                  duration: 2000         
                })              
            } else {
                _this.showErrorFn(resData.message)
            }
        }
      })
    },
    getSettledFn: function () {
        let _this = this;
        let token = this.data.token;
        let url = app.globalData.url;
        let userInfo = this.data.userInfo;
        let personInfo = this.data.personInfo;
        let licenseInfo = this.data.licenseInfo;
        wx.showLoading({
          title: '加载中'
        })
        wx.request({
          url: url, 
          method: "get",
          data: {
            api: "app.user.getBusinessInfo",
            storeId: 1,
            storeType: 6,
            accessId: token
          },
          header: {
            'content-type': 'application/json'  
          },
          success (res) {
              wx.hideLoading()
              let resData = res.data;
              if (res.data.code == 200) {
                  let resData = res.data.data;
                  userInfo.submitFlag = resData.submitFlag;
                  userInfo.examineFlag = resData.examineFlag;
                  userInfo.signatoryName = resData.signatoryName;
                  //  userInfo.submitFlag = 0;
                  //  userInfo.examineFlag = 0;
                  wx.setStorageSync('userInfo', userInfo);
                  _this.setData({
                      submitInfo: resData,
                      submitFlag: resData.submitFlag,
                      examineFlag: resData.examineFlag,
                      industryIndex: resData.industry || 0,
                      userType: resData.signatoryType 
                  })
                  _this.init()
              }
          }
        })      
    }
})